﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class DuctAlignModel : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new NotImplementedException();
				}
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				RevitVersionFuncs.ClearSelection(selection);
				string text = "风管对齐样式";
				transaction = new Transaction(document, text);
				transaction.Start();
				new List<Duct>();
				new List<XYZ>();
				ISelectionFilter selectionFilter = new DuctSelectionFilter();
				Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择起始点");
				Duct duct = document.GetElement(reference) as Duct;
				reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择终点");
				Duct duct2 = document.GetElement(reference) as Duct;
				MessageBox.Show(new HYDuctAlignment().judgeTwoDuctAlignmentModel(duct, duct2).ToString());
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception)
			{
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (1 == (int)transaction.GetStatus())
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		private double mmToFeet(double mm)
		{
			return mm * 0.0032808399;
		}
	}
}
